package threadjuc.stream;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

/**
 * @author zxl
 * @version 1.0
 * @date 2021/10/28 14:01
 */
public class stream1 {
    public static void main(String[] args) {
        User a = new User(1, "a", 20);
        User b = new User(2, "b", 21);
        User c = new User(3, "c", 22);
        User d = new User(4, "d", 23);
        User e = new User(5, "e", 24);
        User f = new User(6, "f", 25);

        List<User> list = Arrays.asList(a,b,c,d,e,f);

        //条件过滤    注意使用System.out::println后流将自动关闭
        Stream<User> userStream = list.stream().filter((user) -> user.getId() % 2 == 0);
        userStream.forEach(System.out::println);
        System.out.println();


        //转换
        Stream<String> stringStream = list.stream().map((user) -> user.getName().toUpperCase());
        stringStream.forEach(System.out::print);
        System.out.println();

        //跳过
        Stream<User> skip = list.stream().skip(2);
        skip.forEach(System.out::println);
        System.out.println();

        //排序
        Stream<String> sorted = list.stream().map((user -> user.getName().toUpperCase())).sorted((u1,u2)->u2.compareTo(u1));
        sorted.forEach(System.out::print);

        //将Stream转为List
        List<String> strings = sorted.collect(Collectors.toList());
        for (String s : strings) {
            System.out.println(s);
        }


    }
}

class User{
    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
